package com.coollang.smartbra.views;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.View;

import com.coollang.smartbra.R;
import com.coollang.smartbra.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


public class FlowerView extends View {
    public static String TAG = "FlowerView";

    Bitmap mFlowers = null;
    List<Bitmap> Flowers = new ArrayList<Bitmap>();
    MyFlower flowers[] = new MyFlower[40];//50片雪花
    int[] ids = new int[]{R.drawable.hua1
            , R.drawable.hua2
            , R.drawable.hua3
            , R.drawable.hua4
            , R.drawable.hua5
            , R.drawable.hua6
            , R.drawable.hua7
            , R.drawable.hua8
            , R.drawable.hua9
            , R.drawable.hua10
            , R.drawable.hua11
            , R.drawable.hua12
            , R.drawable.hua13
            , R.drawable.hua14
            , R.drawable.hua15
            , R.drawable.hua16
            , R.drawable.hua17
            , R.drawable.hua18
            , R.drawable.hua19
            , R.drawable.hua20
            , R.drawable.hua21
            , R.drawable.hua22
            , R.drawable.hua23
            , R.drawable.hua24
            , R.drawable.hua25
            , R.drawable.hua26
            , R.drawable.hua27
            , R.drawable.hua28
            , R.drawable.hua29
            , R.drawable.hua30
            , R.drawable.hua31
            , R.drawable.hua32
            , R.drawable.hua33
            , R.drawable.hua34
            , R.drawable.hua35
            , R.drawable.hua36
            , R.drawable.hua37
            , R.drawable.hua38
            , R.drawable.hua39
            , R.drawable.hua40
    };
    private Integer[] offsetX;
    private Integer[] offsetY;
    Random r = new Random();
    Matrix m = new Matrix();
    Paint p = new Paint();

    int mW = 1440;
    int mH = 2560;
    float de = 0f;

    public void setWH(int pW, int pH, float de) {
        LogUtils.e(TAG, pW + "========================");
        this.mW = pW;
        this.mH = pH;
        this.de = de;
//        System.out.println(de ----> + de);
        offsetX = new Integer[]{(int) (2 * de), (int) (-2 * de), (int) (-1 * de), 0, (int) (1 * de), (int) (2 * de), (int) (1 * de)};
        offsetY = new Integer[]{(int) (3 * de), (int) (5 * de), (int) (5 * de), (int) (3 * de), (int) (4 * de)};
    }

    public FlowerView(Context context) {
        super(context);
    }

    public FlowerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public FlowerView(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < flowers.length; i++) {
            MyFlower rect = flowers[i];
            int t = rect.t;
            t--;
            if (t <= 0) {
                rect.y += rect.g;
                canvas.save();
                m.reset();
                m.setScale(rect.s, rect.s);
                canvas.setMatrix(m);
                p.setAlpha(rect.a);
                canvas.drawBitmap(Flowers.get(i), rect.x, rect.y, p);
                canvas.restore();
            }
            rect.t = t;
            if (rect.y >= mH) {
                rect.init();
            }
            if (rect.x >= mW || rect.x < -20) {
                rect.init();
            }
            flowers[i] = rect;
        }
    }


    public void loadFlower() {
        Resources r = this.getContext().getResources();
        mFlowers = ((BitmapDrawable) r.getDrawable(R.drawable.icon_pbzc)).getBitmap();
        for (int i = 0; i < ids.length; i++) {
            Flowers.add(((BitmapDrawable) r.getDrawable(ids[i])).getBitmap());
        }
    }

    public void recly() {
        if (mFlowers != null && !mFlowers.isRecycled()) {
            mFlowers.recycle();
        }
    }

    public void addRect() {
        for (int i = 0; i < flowers.length; i++) {
            flowers[i] = new MyFlower();
        }
    }

    public void inva() {
        invalidate();
    }


    class MyFlower {
        int x;
        int y;
        float s;
        int a;
        int t;
        int g;

        public void init() {
            float aa = r.nextFloat();
            this.x = r.nextInt(mW) + 200;
            this.y = 0;
            if (aa >= 1) {
                this.s = 1.1f;
            } else if (aa <= 0.5) {
                this.s = 1.0f;
            } else {
                this.s = aa;
            }
            this.a = r.nextInt(155) + 100;
            this.t = r.nextInt(105) + 1;
            this.g = offsetY[r.nextInt(4)];
        }

        public MyFlower() {
            super();
            init();
        }

    }

}